<template>
  <div class="container">
    <nav-bar title="品牌主理人" v-if="!isISP" />
    <!-- 用户信息 -->
    <div class="user-infor" @click="goToShopBase">
      <img v-if="userInfoDetail.Photo==='' || userInfoDetail.Photo===null" src='../../assets/img/person.png' />
      <img v-else :src='userInfoDetail.Photo' />
      <div class="infor">
        <h3 v-if="!isISP">{{userInfoDetail.Nick}}</h3>
        <h3 v-else>{{userInfoDetail.ShopName}}</h3>
        <p v-if="!isISP">TITIKA认证品牌主理人</p>
        <p v-else>TITIKA城市运营商</p>
      </div>
      <div class="arrow"></div>
    </div>
    <!-- 导航链接 -->
    <div class="user-navs">
      <!-- @click="goToBenefit(1)" -->
      <div class="item" @click="goToBenefit(1)">
        <div class="label">累计收益</div><!-- 已提现 -->
        <div class="value">{{userBenefitDetail.WithdrawalsAmount}}元</div>
        <div class="arrow"></div>
      </div>
      <router-link to="/benefitWithdraw" class="item">
      <!-- <div class="item"> -->
        <div class="label">可用收益</div>
        <!-- <div class="value">{{userBenefitDetail.WithdrawalsAmount}}元</div> -->
        <div class="value">{{userBenefitDetail.Balance}}元</div>
        <div class="arrow"></div>
      <!-- </div> -->
      </router-link>
      <div class="item" @click="goToBenefit(0)">
        <div class="label">待收益</div>
        <div class="value">{{userBenefitDetail.NoSettlementAmount}}元</div>
        <div class="arrow"></div>
      </div>
      <router-link to="/payment" class="item" style="display: none">
      <!-- <div class="item"> -->
        <div class="label">收款账户管理</div>
        <div class="value"></div>
        <div class="arrow"></div>
      <!-- </div> -->
      </router-link>
      <router-link to="/fans" class="item item-mt">
        <div class="label" v-if="!isISP">我的粉丝</div>
        <div class="label" v-else>我的主理人</div>
        <div class="value">{{ myFans.length }}</div>
        <div class="arrow"></div>
      </router-link>
      <!-- <router-link to="/shop" class="item"> -->
      <div class="item" @click="goToLife(ParentMember.Id)" v-if="!isISP">
        <div class="label">归属</div>
        <div class="value" v-if="ParentMember">{{ParentMember.Nick}}</div>
        <div class="arrow" v-if="!youISP"></div>
      </div>
      <!-- </router-link> -->
      <div class="item item-mt" @click="share">
        <div class="label">我的关联码</div>
        <div class="value"></div>
        <div class="arrow"></div>
      </div>
      <router-link to="/shop" class="item" v-if="!isISP">
        <div class="label">门店管理</div>
        <div class="value">{{shopInfo.ShopBranchName}}</div>
        <div class="arrow"></div>
      </router-link>
      <!-- <router-link to="/" class="item">
        <div class="label">收益排行榜</div>
        <div class="value"></div>
        <div class="arrow"></div>
      </router-link> -->
      <router-link v-if="isISP && userInfoDetail.UserId === 699" to="/regnewShop" class="item">
        <div class="label">城市运营商申请</div>
        <div class="value"></div>
        <div class="arrow"></div>
      </router-link>
      <router-link v-if="isISP && userInfoDetail.UserId === 699" to="/coupon" class="item">
        <div class="label">内部优惠券</div>
        <div class="value"></div>
        <div class="arrow"></div>
      </router-link>
      <div class="item item-mt" v-if="userKey" @click="logout">
        <div class="label">退出登录</div>
        <div class="arrow"></div>
      </div>
    </div>
    <!-- 分享展示层 -->
    <div class="share-banner" v-show="shareBannerShow">
      <div class="banner" id="shareDom">
        <img class="imglogo" src='../../assets/img/share-csyys-tit.jpg' />
        <img class="imglogo" src='../../assets/img/share-csyys-img.jpg' />
        <!-- <img v-if="!isISP" class="imgpro" :src="shopInfo.ShopImages" />
        <img v-else class="imgpro" src="../../assets/img/csyys.jpg" /> -->
        <div class="qrcode-dis">
          <div class="qrcode" id="qrcode"></div>
          <div class="dis">
            <img v-if="isISP" src='../../assets/img/share-csyys-txt.jpg' />
            <img v-else src='../../assets/img/share-csyys-txt2.jpg' />
          </div>
        </div>
        <!-- <div class="entrybtn">
          长按即刻进入
        </div> -->
      </div>
      <div id="shareRet">
        <van-loading 
          v-if="!shareImg" 
          class="share-loading" 
          size="24px" 
          vertical
        >
          关联码生成中...
        </van-loading>
        <img v-if="shareImg" :src='shareImg' />
      </div>
      <div class="share-btns">
        <div class="item item-1" @touchstart.prevent="press">
          <div class="img"></div>
          <!-- <img src="../../assets/img/icon-hb.png" /> -->
          <h3>长按上图保存</h3>
        </div>
        <div class="item item-2" id="copyBtn">
          <div class="img"></div>
          <!-- <img src="../../assets/img/icon-link.png" /> -->
          <h3>复制链接</h3>
        </div>
      </div>
      <div class="close" @click="closeShare">&times;</div>
    </div>
  </div>
</template>

<script>
import NavBar from 'components/common/NavBar'
import { getUserDetail, getUserDetailById, logout } from '../../network/user'
import { 
  getMyBenefit, 
  getMyBenefitList, 
  getMyShop,
  getMyFans, 
} from '../../network/belong'
import { getShopBranchInfo } from '../../network/shop'
import { drawHtml, copyInit } from '../../utils/share'

export default {
  components: {
    NavBar
  },
  data() {
    return {
      userKey: localStorage.getItem('userKey'),
      isISP: JSON.parse(localStorage.getItem('isISP')),
      ParentMember: JSON.parse(localStorage.getItem('ParentMember')),
      userInfoDetail: {},
      userBenefitDetail: {
        SettlementAmount: 0, // 总结算收入
        Balance: 0, // 可提现
        WithdrawalsAmount: 0, // 已提现 累计收益
        NoSettlementAmount: 0 // 待收益 未结算的佣金
      },
      shopInfo: {},
      shareBannerShow: false,
      shareImg: '',
      myFans: [],
      youISP: '',
      youBelongMan: '',
    }
  },
  async mounted() {
    this.getUserDetail()
    this.getMyBenefit()
    this.getMyBenefitList()
    await this.getMyShop()
    this.getShopById(this.shopInfo.ShopBranchId)
    this.getMyFans()
    copyInit('belong')
  },
  methods: {
    press(e){},
    async goToLife(MemberId){
      let userInfo = await this.getIsISPorBelong(MemberId)
      this.youISP = userInfo.IsISP
      this.youBelongMan = userInfo.IsDistributor

      if(this.youISP){
        return false
      }

      this.$router.push({
        path: '/life',
        query: {
          action: 'you',
          id: MemberId,
        }
      })
    },
    async share(){
      this.shareBannerShow = true
      if(this.shareImg === ''){
        
        this.shareImg = await drawHtml(this, '#shareDom', '#shareRet', 'belong', {
          width: 74,
          height: 74
        })
      }
    },
    closeShare(){
      this.shareBannerShow = false
    },
    getMyShop(){
      return new Promise(resolve => {
        let requestData = {
          userkey: this.userKey
        }
        getMyShop(requestData, shop => {
          this.shopInfo = shop
          resolve()
        })
      })
    },
    getShopById(shopId){
      let requestData = {
        id: shopId,
        fromLatLng: ''
      }
      getShopBranchInfo(requestData, shop => {
        shop = shop.Store
        this.shopInfo = shop
      })
    },
    getUserDetail(){
      let requestData = {
        userkey: this.userKey
      }
      getUserDetail(requestData, async info => {
        this.userInfoDetail = info
        let userInfo = await this.getIsISPorBelong(info.UserId)
        this.$set(this.userInfoDetail, 'ShopName', userInfo.ShopName)
      })
    },
    getMyBenefit(){
      let requestData = {
        userkey: this.userKey
      }
      getMyBenefit(requestData, benefit => {
        this.userBenefitDetail = benefit
      })
    },
    getMyFans(){
      getMyFans( list => {
        this.myFans = list
      })
    },
    getMyBenefitList(){
      let requestData = {
        userkey: this.userKey,
        pageSize: 20,
        pageNo: 1,
      }
      getMyBenefitList(requestData, list => {
        this.userBenefitList = list
      })
    },
    goToBenefit(type){
      this.$router.push({
        path: '/benefit',
        query: {
          type
        }
      })
    },
    logout(){
      logout()
    },
    getIsISPorBelong(userId) {
      return new Promise((resolve) => {
        getUserDetailById(userId, (userInfo) => {
          console.log(userInfo)
          // 判断是否是城市运营商
          // let isISP = userInfo.IsISP
          // let isBelongMan = userInfo.IsDistributor
          console.log(this.userInfoDetail)
          resolve(userInfo)
        })
      })
    },
    goToShopBase(){
      this.$router.push('/shopBase')
    },
  },
}
</script>

<style scoped>
.user-infor{
  padding: 20px 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #ffffff;
  background-color: #000000;
}
.user-infor img{
  width: 45px;
  height: 45px;
  border-radius: 50%;
  margin-right: 25px;
  border: 1px solid #ffffff;
}
.user-infor .infor{
  flex-grow: 1;
}
.user-infor .infor h3{
  font-size: 16px;
  padding-bottom: 6px;
}
.user-infor .infor p{
  font-size: 14px;
}
.user-navs{
  padding-bottom: 10px;
}
.user-navs .item{
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 30px;
  height: 50px;
  font-size: 14px;
  border-top: 1px solid #f7f7f7;
  background-color: #ffffff;
}
.user-navs .item-mt{
  margin-top: 10px;
  border-top: none;
}
.user-navs .item .value{
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-grow: 1;
  color: #8c8c8c;
  text-align: right;
}
.user-navs .item .value .new{
  display: inline-block;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background-color: #d50721;
}
.user-navs .item .arrow{
  width: 6px;
  height: 6px;
  border-top: 1px solid #8c8c8c;
  border-right: 1px solid #8c8c8c;
  transform: rotate(45deg);
  margin-left: 10px;
}

.share-banner{
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  box-sizing: border-box;
  background-color: #fff;
  z-index: 1000;
}
.share-banner .banner{
  position: relative;
  padding: 40px 20px 20px 20px;
  overflow: hidden;
}
.share-banner .banner .imglogo{
  display: block;
  width: 100%;
  margin: 0 auto;
}
.share-banner .banner .imgpro{
  width: 100%;
  background-color: #f4f4f4;
}
.share-banner .banner .pro-name{
  font-weight: bold;
  line-height: 1.4;
  padding-top: 30px;
}
.share-banner .banner .pro-tips{
  color: #999999;
  font-size: 14px;
  padding: 10px 0 0 0;
}
.share-banner .share-btns{
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 80px;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  z-index: 1001;
}
.share-banner .share-btns .item{
  text-align: center;
  /* flex-grow: 1; */
  width: 50%;
}
.share-banner .share-btns .item .img{
  display: inline-block;
  width: 35px;
  height: 35px;
  margin-bottom: 5px;
  background: url(../../assets/img/icon-hb.png);
  background-size: 100% auto;
}
.share-banner .share-btns .item-2 .img{
  background: url(../../assets/img/icon-link.png);
  background-size: 100% auto;
}
.share-banner .qrcode-dis{
  display: flex;
  align-items: flex-end;
  padding-top: 20px;
}
.share-banner .qrcode-dis .qrcode{
  width: 74px;
  height: 74px;
  background-color: #fff;
  box-sizing: border-box;
}
.share-banner .qrcode-dis .dis{
  color: #999;
  font-size: 14px;
  flex-grow: 1;
  text-align: right;
  font-size: 16px;
  line-height: 1.8;
  color: #000;
}
.share-banner .qrcode-dis .dis img{
  width: 110px;
}
.share-banner .entrybtn{
  margin-top: 20px;
  line-height: 50px;
  font-size: 14px;
  color: #fff;
  text-align: center;
  background-color: #000;
}
.share-banner #shareRet{
  /* display: none; */
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  z-index: 20;
  background-color: #fff;
}
/* .share-banner #shareRet::-webkit-scrollbar {
  width: 0;
  height: 0;
  -webkit-overflow-scrolling: touch;
  -ms-overflow-style: none;
  overflow: -moz-scrollbars-none;
  background-color: rgba(240,240,240,0)
} */
.share-banner .share-loading{
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -20px 0 0 -40px;
}
.share-banner #shareRet img{
  width: 100%;
  vertical-align: baseline;
}
.close{
  position: absolute;
  top: 12px;
  right: 13px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  text-align: center;
  line-height: 22px;
  color: #fff;
  font-size: 20px;
  z-index: 21;
  background-color: #000;
}
.arrow{
  width: 8px;
  height: 8px;
  border-radius: 2px;
  border-right: 2px solid #fff;
  border-top: 2px solid #fff;
  position: relative;
  transform: rotate(45deg);
}
</style>